package org.alex.samples.service;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.alex.samples.dao.IScoreDao;
import org.alex.samples.dao.ISubjectDao;
import org.alex.samples.dao.ITestDao;
import org.alex.samples.dao.ITestTypeDao;
import org.alex.samples.model.ExamScore;
import org.alex.samples.model.ExamScoreVO;
import org.alex.samples.model.ExamTest;
import org.alex.samples.model.ExamTestType;
import org.alex.samples.model.ExamTestTypeVO;
import org.alex.samples.model.ExamTestVO;
import org.alex.samples.model.PagerModel;
import org.alex.samples.model.Subject;
import org.alex.samples.model.SubjectVO;
import org.alex.samples.util.TestUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.EnableTransactionManagement;
import org.springframework.transaction.annotation.Transactional;

@Service
@Transactional
public class ManageTestServiceImpl implements ManageTestService {

	@Autowired
	private ITestDao testDao;

	@Autowired
	private ISubjectDao subjectDao;

	@Autowired
	private ITestTypeDao testTypeDao;
	
	@Autowired
	IScoreDao scoreDao;

	@Override
	public List<ExamTestVO> findAllTestsAndSub() throws Exception {
		List<ExamTest> tests = testDao.findAllTests();
		List<ExamTestVO> testVOs = new ArrayList<ExamTestVO>();
		for (int i = 0; i < tests.size(); i++) {
			ExamTestVO testVO = new ExamTestVO();
			BeanUtils.copyProperties(tests.get(i), testVO);
			Subject subject = subjectDao.findSubjectById(testVO.getSubID());
			SubjectVO subjectVO = new SubjectVO();
			if (subject != null) {
				BeanUtils.copyProperties(subject, subjectVO);
				testVO.setSubject(subjectVO);
			}
			testVOs.add(testVO);
		}
		return testVOs;
	}

	@Override
	public ExamTestVO findByIDTestAndSub(Integer id) throws Exception {
		ExamTest test = testDao.getTestById(id);
		if (test != null) {
			ExamTestVO testVO = new ExamTestVO();
			BeanUtils.copyProperties(test, testVO);
			Subject subject = subjectDao.findSubjectById(test.getSubID());
			SubjectVO subjectVO = new SubjectVO();
			if (subject != null) {
				BeanUtils.copyProperties(subject, subjectVO);
				testVO.setSubject(subjectVO);
			}
			return testVO;
		}
		return null;
	}

	@Override
	public void editTest(ExamTest examTest) throws Exception {
		testDao.editTest(examTest);
		scoreDao.flush();
	}

	@Override
	public void createTest(ExamTest examTest) throws Exception {
		testDao.createTest(examTest);
		scoreDao.flush();
	}

	@Override
	public void deleteTest(Integer testId) throws Exception {
		testDao.deletetTest(testId);
		scoreDao.flush();

	}

	public PagerModel<ExamTestVO> findAllByPage() throws Exception {
		PagerModel<ExamTestVO> pm = testDao.findAllByPage();
		for (int i = 0; i < pm.getDatas().size(); i++) {
			Subject subject = subjectDao.findSubjectById(pm.getDatas().get(i).getSubID());
			SubjectVO subjectVO = new SubjectVO();
			if (subject != null) {
				BeanUtils.copyProperties(subject, subjectVO);
				pm.getDatas().get(i).setSubject(subjectVO);
			}
		}
		return pm;
	}

	@Override
	public PagerModel<ExamTestVO> findTestBySubId(Integer id) throws Exception {
		PagerModel<ExamTestVO> pm = testDao.findSubTestByPage(id);
		List<ExamTestVO> testVOs = new ArrayList<ExamTestVO>();
		for (int i = 0; i < pm.getDatas().size(); i++) {
			if (pm.getDatas().get(i).getSubID() == id) {
				Subject subject = subjectDao.findSubjectById(pm.getDatas().get(i).getSubID());
				SubjectVO subjectVO = new SubjectVO();
				if (subject != null) {
					BeanUtils.copyProperties(subject, subjectVO);
					pm.getDatas().get(i).setSubject(subjectVO);
				}
				testVOs.add(pm.getDatas().get(i));
			}
		}
		pm.setDatas(testVOs);
		return pm;
	}

	@Override
	public List<ExamTestType> findAllTestType() throws Exception {
		return testTypeDao.findAllTestType();

	}

	@Override
	public ExamTestType getTestTypeById(Integer id) throws Exception {
		return testTypeDao.getExamTestTypeById(id);
	}

	@Override
	public Map<Integer, List<ExamTestVO>> getTestListBySubject(Integer subId, String userId) throws Exception {
		List<ExamTestType> testTypeList = testTypeDao.findAllTestType();
		Map<Integer, Integer> testTypeScoreMap = new HashMap<Integer, Integer>(); // 所有题型各自的分数
		Map<Integer, List<ExamTestVO>> testListMap = new HashMap<Integer, List<ExamTestVO>>();
		// 获取各个题型的数量
		Map<Integer, Integer> testTypeCountMap = new HashMap<Integer, Integer>();
		// 获取所以题型的得到
		if (testTypeList != null) {
			for (ExamTestType testType : testTypeList) {
				int typeCount=0;
				testTypeScoreMap.put(testType.getId(), testType.getTestTypeScore());
				// 根据科目获取相应的所以题目
				List<ExamTest> testList = testDao.getTestListBySubAndTestType(subId, testType.getId());
				List<ExamTestVO> testVOList = new ArrayList<ExamTestVO>();
				for (ExamTest test : testList) {
					typeCount++;
					ExamTestVO testVO = new ExamTestVO();
					BeanUtils.copyProperties(test, testVO);
					ExamTestTypeVO testTypeVO = new ExamTestTypeVO();
					BeanUtils.copyProperties(test.getExamTestType(), testTypeVO);
					testVO.setExamTestType(testTypeVO);
					testVOList.add(testVO);
				}
				testTypeCountMap.put(testType.getId(), typeCount);
				testListMap.put(testType.getId(), testVOList);
			}
		} else {
			return null;
		}
		Subject subject = subjectDao.findSubjectById(subId);
		if (subject == null) {
			return null;
		}
		Integer totalTestScore = (int) subject.getTotal_score();
		Integer totalTestCount = subject.getTest_count();
		//  生成随机题目
		Map<Integer, List<ExamTestVO>> resultTestListMap = new HashMap<Integer, List<ExamTestVO>>();

		for (Integer key : testTypeCountMap.keySet()) {
			Integer count=null;
			if(key==1){
				count=10;
			}else if(key==2){
				count=5;
			}
			List<ExamTestVO> testList = testListMap.get(key);
			List<Integer> randomTestIdList = TestUtils.getRandomTestIdList(testList, count);
			if (randomTestIdList == null) {
				return null;
			}
			List<ExamTest> randomTestList=testDao.getTestListByIdList(randomTestIdList);
			// 转换List<ExamTest> ==>List<ExamTestVO>
			List<ExamTestVO> testVOList = new ArrayList<ExamTestVO>();
			for(ExamTest test : randomTestList){
				ExamTestVO vo = new ExamTestVO();
				BeanUtils.copyProperties(test, vo);
				ExamTestTypeVO testTypeVO = new ExamTestTypeVO();
				BeanUtils.copyProperties(test.getExamTestType(), testTypeVO);
				vo.setExamTestType(testTypeVO);
				testVOList.add(vo);
			}
			resultTestListMap.put(key, testVOList);
		}	
		return resultTestListMap;
	}

	@Override
	public ExamScoreVO checkAnswerAndSaveScore(Integer subId, String userId, List<Integer> answerIds,
			Map<String, String> testMap) throws Exception {
		List<ExamTest> testList=testDao.getTestListByIdList(answerIds);
		double scoreCount=0;
		for(ExamTest test:testList ){
			String modelAnswer=test.getAnswer();
			String stuAnswer=testMap.get(test.getId().toString());
			if(modelAnswer.equals(stuAnswer)){
				scoreCount+=test.getExamTestType().getTestTypeScore();
			}
		}
		ExamScore score=new ExamScore();
		score.setUserId(userId);
		score.setSubjectId(subId);
		score.setExamTime(new Date());
		score.setScore(scoreCount);
		scoreDao.addScore(score);
		scoreDao.flush();
		ExamScoreVO vo=new ExamScoreVO();
		BeanUtils.copyProperties(score, vo);
		return vo;
	}

}
